package org.dromara.module.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.module.domain.Material;
import org.dromara.module.service.IMaterialService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 素材库
 *
 * @author Mr.Zhou
 * @date 2024-10-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/module/material")
public class MaterialController extends BaseController {

    private final IMaterialService materialService;

    @Value("${ruoyi.imgUrl}")
    private String imgUrl;
    /**
     * 查询素材库列表
     */
//    @SaCheckPermission("shopping:material:list")
    @GetMapping("/list")
    public TableDataInfo<Material> list(Material material, PageQuery pageQuery) {
        QueryWrapper<Material> qw = new QueryWrapper<>();
        if(material.getGroupId()!=null){
            qw.eq("group_id",material.getGroupId());
        }
        qw.orderByDesc("create_time");

        TableDataInfo<Material> td = PageUtils.buildDataInfo(materialService.page(PageUtils.buildPage(),qw));
        List<Material> list = td.getRows();
        if(CollUtil.isNotEmpty(list)){
            list.forEach(material1 -> {
                if(StrUtil.isNotEmpty(material1.getUrl()) && (!material1.getUrl().contains("https") && !material1.getUrl().contains("http"))) {
                    material1.setImgUrl(imgUrl + material1.getUrl());
                }
            });
        }
        return td;
    }


    /**
     * 获取素材库详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("shopping:material:query")
    @GetMapping("/{id}")
    public R<Material> getInfo(@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(materialService.getById(id));
    }

    /**
     * 新增素材库
     */
    @SaCheckPermission("shopping:material:add")
    @Log(title = "素材库", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody Material material) {
        material.setCreateTime(new Date());
//		material.setId(UUID.randomUUID().toString());

        if(StrUtil.isNotEmpty(material.getUrl())){
            String[] imgs = material.getUrl().split("\\.");
            material.setFileType(imgs[1].toUpperCase());
        }
        return toAjax(materialService.save(material));
    }

    /**
     * 修改素材库
     */
    @SaCheckPermission("shopping:material:edit")
    @Log(title = "素材库", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody Material material) {
        if(StrUtil.isNotEmpty(material.getUrl())){
            String[] imgs = material.getUrl().split("\\.");
            material.setFileType(imgs[1].toUpperCase());
        }
        return toAjax(materialService.updateById(material) ? 1 : 0);
    }

    /**
     * 删除素材库
     *
     * @param ids 主键串
     */
    @SaCheckPermission("shopping:material:remove")
    @Log(title = "素材库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable String[] ids) {
        return toAjax(materialService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
